package com.xjgc.common.utils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import javax.ws.rs.core.MediaType;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class PushDataUtil {
    public static String pushData(String url, String orisys,String transno,String operateType,Object object){
        RestTemplate restTemplate=new RestTemplate();
        for (HttpMessageConverter<?> httpMessageConverter : restTemplate.getMessageConverters()) {
            if (httpMessageConverter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("UTF-8"));
            }
        }
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("orisys",orisys);
        httpHeaders.add("transno",transno);
        httpHeaders.add("operateType",operateType);
        httpHeaders.add("token","111111111111");
        httpHeaders.setContentType(org.springframework.http.MediaType.parseMediaType("application/json; charset=UTF-8"));
        httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
        httpHeaders.set(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, "application/json");
        httpHeaders.set(javax.ws.rs.core.HttpHeaders.ACCEPT_CHARSET, StandardCharsets.UTF_8.toString());
        String jsonString= JSONObject.toJSONString(object, SerializerFeature.WriteMapNullValue);
        jsonString=jsonString.replaceAll("(\\\\r\\\\n)","");
        jsonString=jsonString.replaceAll("workShop","workshop").replaceAll("null","\"\"").replaceAll("matrialsCode","materialsCode");
       if(url.contains("supplier-production-schedule")||url.contains("supplier-process-work")){
           jsonString=jsonString.replaceAll("puchaserHqCode","purchaserHqCode");
       }
       if(url.contains("supplier-process-work")){
            jsonString=jsonString.replaceAll("actualEndTime","actualFinishDate");
           jsonString=jsonString.replaceAll("actualStartTime","actualStartDate");
       }
        HttpEntity request = new HttpEntity(jsonString, httpHeaders);
        String result="";
        int status = 0 ;
        try {
            ResponseEntity resp = restTemplate.postForEntity(url,request,String.class);
            //ResponseEntity resp = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
            result = resp.getBody().toString();
            status = resp.getStatusCode().value();
            System.out.println(status+"<-->"+result);
        } catch (Exception e) {
            System.out.println("推送发生异常：url = [" + url + "], orisys = [" + orisys + "], transno = [" + transno + "], operatetype = [" + operateType + "], object = [" + object + "]");
            e.printStackTrace();
        }
        if (status==200){
            return result;
        }else{
            return null;
        }
    }

    public static void main(String[] args) {
//        String str="{'purchaserHqCode':'sgcc','soNo':'4000382614','supplierCode':'1000069096\\r\\n','buyerCode':'1023454566\\r\\n','buyerProvince':'浙江\\r\\n','soRelType':'0\\r\\n','categoryCode':'25','subclassCode':'01','soStatus':'1','dataSource':'0','dataSourceCreateTime':'2019-08-23','isDeleted':'0','supplierSoNo':'sgcc','soItemNo':'004000','supplierSoRelType':'1','productCode':'000000000703739670','productName':'三相表','productUnit':'台','productAmount':'5980.000','productIdGrpNo':null,'planDeliveryDate':'2019-12-30','planDeliveryPlace':'山东电子','planDeliveryPlaceDesc':'sgcc','ioflag':'10004','message':'销售订单推送数据:销售订单对应关系字段数值不正确,请检查后重新推送','status':'0','transno':'20191120161200459183403','orderId':15,'eipPoNo':'0','ownerId':'1','openId':'1'}";
//        str=str.replaceAll("(\\\\r\\\\n)","");
//        System.out.println(str);
        Integer i= Integer.parseInt("123.000");
        System.out.println(i);
    }
}
